#include "mywindow.h"

mywindow::mywindow(QWidget *parent) : QWidget(parent)
{
    this->setGeometry(100,100, 300,500);

    lcd = new QLCDNumber(this);
    lcd->setGeometry(100,30, 100,40);

    btnStart = new QPushButton(this);
    btnStart->setText("Start");
    btnStart->setGeometry(100,100, 100,30);

    btnStop = new QPushButton(this);
    btnStop->setText("Clear");
    btnStop->setGeometry(100,150, 100,30);

    timer = new QTimer(this);
    timer->setInterval(1000);

    second = 0;

    line = new QLineEdit(this);
    line->setGeometry(100,70, 100,30);

    rad = new QRadioButton(this);
    rad->setGeometry(100,180, 100,30);
    rad->setText("reverce");
    rad->setChecked(false);

    connect(timer,SIGNAL(timeout()),this,SLOT(time()));
    connect(btnStart,SIGNAL(pressed()),this,SLOT(start()));
    connect(btnStop,SIGNAL(pressed()),this,SLOT(clear()));
    connect(line,SIGNAL(textEdited(QString)),this,SLOT(change(QString)));
}

void mywindow::time() {
    if (rad->isChecked()) {
        second--;
        lcd->display(second);
    } else {
        second++;
        lcd->display(second);
    }
}

void mywindow::start() {
    QString second = this->line->text();
    if (timer->isActive()) {
        timer->stop();
        btnStart->setText("Start");
    } else {
        timer->start();
        btnStart->setText("Stop");
    }
}

void mywindow::clear() {
    timer->stop();
    btnStart->setText("Start");
    second = this->line->text().toInt();
    lcd->display(second);
}

void mywindow::change(QString newText) {
    timer->stop();
    btnStart->setText("Start");

    second = newText.toInt();
    lcd->display(second);
}
